<?php
/**
*
* @package ppkBB3cker
* @version $Id: posting_addfields_add2.php 1.000 2009-03-25 11:41:00 PPK $
* @copyright (c) 2009 PPK
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

$addfields_sql=$addfields_count=array();
$is_submitted=$posting_page && $submit ? 1 : 0;
$return_page=$is_submitted ? '<br /><br />'.sprintf($user->lang['RETURN_PAGE'], '<a href="'.append_sid("{$phpbb_root_path}posting.{$phpEx}", "mode={$mode}&amp;f={$forum_id}&amp;p={$post_id}", true, $user->session_id).'">', '</a>') : '';
$forum_addfields=0;

if($posting_page && $topic_id && $mode=='edit' && !$preview && !$submit)
{
	$result=$db->sql_query("SELECT field_name, field_value FROM ".TRACKER_ADDFIELDS_TABLE." WHERE post_msg_id='{$post_id}'");
	while($row=$db->sql_fetchrow($result))
	{
		if(@!$user->lang['TRACKER_FORUM_ADDFIELDS_MULTI'][$row['field_name']])
		{
			$addfields_sql[$row['field_name']]=$row['field_value'];
		}
		else
		{
			$addfields_sql[$row['field_name']]=explode(', ', $row['field_value']);
		}
	}
	$db->sql_freeresult($result);
}

foreach($array_addfields as $k)
{
	$v='';
	$c=intval(@$user->lang['TRACKER_FORUM_ADDFIELDS_COUNT'][$k]);
	$field_name=@$user->lang['TRACKER_FORUM_ADDFIELDS_REQ'][$k] ? '<u>'.@$user->lang['TRACKER_FORUM_ADDFIELDS'][$k].'</u>'.(($c || @$user->lang['TRACKER_FORUM_ADDFIELDS_MULTI'][$k]) && $user->lang['TRACKER_FORUM_ADDFIELDS_REQ'][$k] ? ' ('.$user->lang['TRACKER_FORUM_ADDFIELDS_REQ'][$k].')' : '') : @$user->lang['TRACKER_FORUM_ADDFIELDS'][$k];

	if(@$user->lang['TRACKER_FORUM_ADDFIELDS_FIELDS'][$k])
	{
		$addfields_type='TRACKER_FORUM_ADDFIELDS_FIELDS';
	}
	else if(@$user->lang['TRACKER_FORUM_ADDFIELDS_TA'][$k])
	{
		$addfields_type='TRACKER_FORUM_ADDFIELDS_TEXTAREA';
	}
	else if(@$user->lang['TRACKER_FORUM_ADDFIELDS'][$k])
	{
		$addfields_type='TRACKER_FORUM_ADDFIELDS_TEXT';
	}
	if($field_name)
	{
		$template->assign_block_vars('addfields_option', array(
			'ADDFIELDS_DESCR'=>$k,
			'ADDFIELDS_NAME'=>$field_name.(@$user->lang['TRACKER_FORUM_ADDFIELDS_TITLE'][$k] ? '<a class="my_tt" title="'.$user->lang['TRACKER_FORUM_ADDFIELDS_TITLE'][$k].'"><img src="'.$phpbb_root_path.'images/tracker/activity.png" /></a>' : ''),
			'ADDFIELDS_VALUE'=>$c ? build_addfields_multi($user->lang[$addfields_type], $k, $v, $c) : build_addfields_single($user->lang[$addfields_type], $k, $v, $c),
			)
		);
		$forum_addfields=1;
	}
}

if($forum_addfields)
{
	$template->assign_vars(array(
		'FORUM_ADDFIELDS'=>$forum_addfields,
		)
	);
}

if($is_submitted)
{
	$addfields_error='';
	if($addfields_count)
	{
		$addfields_error=array();
		foreach($addfields_count as $k => $v)
		{
			if($v==-1)
			{
				$addfields_error[]=$user->lang['ADDFIELDS_REQUIRED_FIELD'].' <u>'.$user->lang['TRACKER_FORUM_ADDFIELDS'][$k].'</u>';
			}
			else if($v=='x')
			{
				$addfields_error[]=sprintf($user->lang['ADDFIELDS_REQUIRED_EXISTS'], $user->lang['TRACKER_FORUM_ADDFIELDS'][$k]);
			}
			else if($v)
			{
				$addfields_error[]=sprintf($user->lang['ADDFIELDS_REQUIRED_MIN'], $v, ' <u>'.$user->lang['TRACKER_FORUM_ADDFIELDS'][$k]).'</u>'.(@$user->lang['TRACKER_FORUM_ADDFIELDS_EXISTS'][$k] ? ', '.sprintf($user->lang['ADDFIELDS_REQUIRED_EXISTS'], $k) : '');
			}
		}
		//trigger_error('<b>'.$user->lang['ADDFIELDS_REQUIRED'].'</b>: '.implode(', ', $addfields_error).$return_page);
		$error[]='<b>'.$user->lang['ADDFIELDS_REQUIRED'].'</b>: '.implode(', ', $addfields_error);
	}
	if(!$error && !$addfields_error)
	{
		$addfields_sqls=array();
		if($addfields_sql)
		{
			foreach($addfields_sql as $k => $v)
			{
				if(is_array($v) && @$user->lang['TRACKER_FORUM_ADDFIELDS_MULTI'][$k])
				{
					$v=implode(', ', $v);
				}
				if(@$user->lang['TRACKER_FORUM_ADDFIELDS_SUBJSTRING'])
				{
					$user->lang['TRACKER_FORUM_ADDFIELDS_SUBJSTRING']=str_replace("#{$k}#", $v, $user->lang['TRACKER_FORUM_ADDFIELDS_SUBJSTRING']);
				}
				$v=str_replace('%', '%%', $v);
				$addfields_sqls[]="INSERT INTO ".TRACKER_ADDFIELDS_TABLE." (post_msg_id, topic_id, field_name, field_value) VALUES ('%1$"."d', '%2$"."d', '".htmlspecialchars($db->sql_escape($k))."', '".htmlspecialchars($db->sql_escape($v))."')";
			}
		}
		if(@$user->lang['TRACKER_FORUM_ADDFIELDS_SUBJSTRING'])
		{
			$user->lang['TRACKER_FORUM_ADDFIELDS_SUBJSTRING']=preg_replace('/{[^{]*#\w+#[^{]*}/', '', $user->lang['TRACKER_FORUM_ADDFIELDS_SUBJSTRING']);
			$user->lang['TRACKER_FORUM_ADDFIELDS_SUBJSTRING']=preg_replace('/#\w+#/', '', $user->lang['TRACKER_FORUM_ADDFIELDS_SUBJSTRING']);
			$user->lang['TRACKER_FORUM_ADDFIELDS_SUBJSTRING']=str_replace(array('{', '}'), '', $user->lang['TRACKER_FORUM_ADDFIELDS_SUBJSTRING']);
			$user->lang['TRACKER_FORUM_ADDFIELDS_SUBJSTRING']=preg_replace('/\s+/', ' ', $user->lang['TRACKER_FORUM_ADDFIELDS_SUBJSTRING']);
			trim($user->lang['TRACKER_FORUM_ADDFIELDS_SUBJSTRING']) ? $_REQUEST['subject']=$user->lang['TRACKER_FORUM_ADDFIELDS_SUBJSTRING'] : '';
		}
		unset($addfields_sql);
	}
}

//############################################################

function build_addfields_multi($l, $k, $v, $c)
{
	global $addfields_sql, $addfields_count, $user, $is_submitted, $error, $return_page;

	$b_fields='';
	$exists_check=0;

	if(intval($c))
	{
		$tmp_count=$tmp_count2=0;
		$addfields_hlp=@$user->lang['TRACKER_FORUM_ADDFIELDS_HLP'][$k];
		$addfields_checkas=@$user->lang['TRACKER_FORUM_ADDFIELDS_CHECKAS'][$k];
		$addfields_req=@$user->lang['TRACKER_FORUM_ADDFIELDS_REQ'][$k];
		$addfields_multi=@$user->lang['TRACKER_FORUM_ADDFIELDS_MULTI'][$k];
		if($addfields_multi)
		{
			return '';
		}
		for($i=1;$i<=$c;$i++)
		{
			$v=utf8_normalize_nfc(@$_POST[$k.$i]);
			if(($v || $addfields_checkas) && $is_submitted)
			{
				if($addfields_checkas && function_exists($addfields_checkas))
				{
					$field_check=$addfields_checkas($v, $error, $user->lang['TRACKER_FORUM_ADDFIELDS'][$k]);
					$v=$field_check;
				}
			}
			if($v)
			{
				$tmp_count+=1;
				if(is_array($addfields_hlp) && @$user->lang['ADDFIELDS_REQUIRED_EXISTS'][$k] && !in_array($v, $addfields_hlp))
				{
					$tmp_count2-=1;
				}
			}
			$v ? $addfields_sql[$k.$i]=$v : $v=html_entity_decode(@$addfields_sql[$k.$i]);
			$b_fields ? $b_fields.='<br />' : '';
			$b_fields.="[{$i}] ".sprintf($l, $k.$i, htmlspecialchars($v));
			$b_fields.=build_hlp_addfields($k, $i);
		}
		if($addfields_req && $tmp_count < $addfields_req)
		{
			$addfields_count[$k]=$addfields_req;
		}
		else if($addfields_req && $tmp_count2 < 0)
		{
			$addfields_count[$k]='x';
		}
	}

	return $b_fields;
}

function build_addfields_single($l, $k, $v, $c)
{
	global $addfields_sql, $addfields_count, $user, $is_submitted, $error, $return_page;

	$b_fields='';
	$exists_check=0;

	if(!intval($c))
	{
		$tmp_count=$tmp_count2=0;
		$addfields_checkas=@$user->lang['TRACKER_FORUM_ADDFIELDS_CHECKAS'][$k];
		$addfields_req=@$user->lang['TRACKER_FORUM_ADDFIELDS_REQ'][$k];
		$addfields_multi=@$user->lang['TRACKER_FORUM_ADDFIELDS_MULTI'][$k];
		$addfields_multi ? $v=@$_POST[$k] : $v=utf8_normalize_nfc(@$_POST[$k]);
		if(($v || $addfields_checkas) && $is_submitted && !$addfields_multi)
		{
			if($addfields_checkas && function_exists($addfields_checkas))
			{
				$field_check=$addfields_checkas($v, $error, $user->lang['TRACKER_FORUM_ADDFIELDS'][$k]);
				$v=$field_check;
			}
		}

		if(!$addfields_multi)
		{
			if($v && @$user->lang['TRACKER_FORUM_ADDFIELDS_EXISTS'][$k])
			{
				$addfields_hlp=$user->lang['TRACKER_FORUM_ADDFIELDS_HLP'][$k];
				if(is_array($addfields_hlp) && !in_array($v, $addfields_hlp))
				{
					$addfields_count[$k]='x';
				}
			}
			if(!$v && $addfields_req)
			{
				$addfields_count[$k]=-1;
			}
			$v ? $addfields_sql[$k]=$v : $v=(@$addfields_sql[$k] ? html_entity_decode($addfields_sql[$k]) : (isset($user->lang['TRACKER_FORUM_ADDFIELDS_DEF'][$k]) ? $user->lang['TRACKER_FORUM_ADDFIELDS_HLP'][$k][$user->lang['TRACKER_FORUM_ADDFIELDS_DEF'][$k]] : ''));
			$b_fields.=sprintf($l, $k, htmlspecialchars($v));
		}
		else
		{
			is_array($v) && sizeof($v) ? @$addfields_sql[$k]=array() : $v=array();
			$b_fields.='<select name="'.$k.'[]" multiple="multiple" style="width:300px;">';
			foreach($user->lang['TRACKER_FORUM_ADDFIELDS_MULTI'][$k] as $m)
			{
				$sel_m='';
				if(in_array($m, $v) || (is_array(@$addfields_sql[$k]) && in_array($m, $addfields_sql[$k])))
				{
					$sel_m=' selected="selected"';
					$addfields_sql[$k][]=$m;
					$addfields_req ? $tmp_count+=1 : '';
				}
				$m=htmlspecialchars($m);
				$b_fields.='<option value="'.$m.'"'.$sel_m.'>'.$m.'</option>';
			}
			$b_fields.='</select>';
			if($addfields_req && $tmp_count < $addfields_req)
			{
				$addfields_count[$k]=$addfields_req;
			}
		}
		$b_fields.=build_hlp_addfields($k);
	}

	return $b_fields;
}

function build_hlp_addfields($k, $c='')
{
	global $user;

	$forum_addfields='';
	if(@$user->lang['TRACKER_FORUM_ADDFIELDS_MULTI'][$k])
	{
		return '<br />'.$user->lang['ADDFIELDS_MULTIHLP'];
	}
	if(is_array(@$user->lang['TRACKER_FORUM_ADDFIELDS_HLP'][$k]))
	{
		$forum_addfields.='<br />'.$user->lang['TRACKER_ADDFIELDS_EXAMPLE'].': <select name="ins_'.$k.$c.'" onchange="this.form.'.$k.$c.'.value=this.form.ins_'.$k.$c.'.value;" style="width:300px;"><option value="" selected="selected"></option>';
		foreach($user->lang['TRACKER_FORUM_ADDFIELDS_HLP'][$k] as $k2=>$v2)
		{
			$user->lang['TRACKER_FORUM_ADDFIELDS_HLP'][$k][$k2]=htmlspecialchars($user->lang['TRACKER_FORUM_ADDFIELDS_HLP'][$k][$k2]);
			$forum_addfields.='<option value="'.$user->lang['TRACKER_FORUM_ADDFIELDS_HLP'][$k][$k2].'">'.(@$user->lang['TRACKER_FORUM_ADDFIELDS_ALIAS'][$k][$k2] ? $user->lang['TRACKER_FORUM_ADDFIELDS_ALIAS'][$k][$k2] : $user->lang['TRACKER_FORUM_ADDFIELDS_HLP'][$k][$k2]).'</option>';
		}
		$forum_addfields.='</select>';
	}
	else if(@$user->lang['TRACKER_FORUM_ADDFIELDS_HLP'][$k])
	{
		$forum_addfields.='<br />'.$user->lang['TRACKER_ADDFIELDS_EXAMPLE'].': ' .$user->lang['TRACKER_FORUM_ADDFIELDS_HLP'][$k];
	}

	return $forum_addfields;
}
?>
